package com.zhongwang.mms.module.material.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.MAdjustmentApply;
import com.zhongwang.mms.gen.entity.MCheckDetail;
import com.zhongwang.mms.gen.entity.MCheckPlan;
import com.zhongwang.mms.module.material.dao.MAdjustmentApplyDao;
import com.zhongwang.mms.module.material.dao.MCheckDetailDao;
import com.zhongwang.mms.module.material.dao.MCheckPlanDao;
import com.zhongwang.mms.module.material.model.MAdjustmentApplyModel;
import com.zhongwang.mms.module.material.model.MCheckDetailModel;
import com.zhongwang.mms.module.material.model.MCheckPlanModel;
import com.zhongwang.mms.module.material.model.MCheckResultCommitSumModel;
import com.zhongwang.mms.module.material.service.MCheckPlanService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description: 库存盘点 控制层
 * @Author: China.wsg
 * @Date: 2020/04/29 08:18
 */
@Controller
@RequestMapping("/material/checkPlan")
public class MCheckPlanController extends BaseController {

    @Autowired
    private MCheckPlanService checkPlanService;
    @Autowired
    private MCheckPlanDao checkPlanDao;
    @Autowired
    private MCheckDetailDao checkDetailDao;
    @Autowired
    private MAdjustmentApplyDao adjustmentApplyDao;

    /**
     * 项目划库存标记列表 页面
     *
     * @return 页面路径
     */
    @GetMapping("/plan")
    @RequiresPermissions({"matinv:matcheckplan:view"})
    public String plan() {

        return "material/matWarManage/CheckPlan/plan";
    }

    /**
     * 查询盘点计划列表
     *
     * @param checkPlanModel 查询条件
     * @return
     */
    @ResponseBody
    @PostMapping("/planList")
    public JsonResult planList(MCheckPlanModel checkPlanModel) {
        startPage();
        List<MCheckPlan> list = checkPlanService.findMChangeOrderByQuery(checkPlanModel);
        return jsonResult(list);
    }

    /**
     * 保存盘点计划
     *
     * @param checkPlan 保存信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions({"matinv:matcheckplan:edit"})
    public JsonResult save(MCheckPlan checkPlan) {
        JsonResult jsonResult = new JsonResult();
        Integer cpnId = checkPlanService.save(checkPlan);
        if (cpnId != null) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        } else {
            jsonResult.setType(JsonResult.ResultType.ERROR);
        }
        jsonResult.setData(cpnId);
        return jsonResult;
    }

    /**
     * 修改盘点计划
     *
     * @param checkPlan 保存信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/edit")
    @RequiresPermissions({"matinv:matcheckplan:edit"})
    public JsonResult edit(MCheckPlan checkPlan) {
        return jsonResult(checkPlanService.edit(checkPlan));
    }

    /**
     * 修改盘点计划状态
     *
     * @param checkPlan 修改信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/updateStatus")
    @RequiresPermissions({"matinv:matcheckplan:edit"})
    public JsonResult updateStatus(MCheckPlan checkPlan) {

        checkPlanService.updateStatus(checkPlan);

        // 取得 盘点整体进度 加载id
        Integer cpnId = null;
        if (checkPlan.getCpnStatus() == 0) {
            // 删除
            List<MCheckPlan> list = checkPlanService.findMChangeOrderByQuery(new MCheckPlanModel());
            if (list.size() > 0) {
                cpnId = list.get(0).getCpnId();
            }
        } else if (checkPlan.getCpnStatus() == 3 || checkPlan.getCpnStatus() == 4) {
            // 提交或撤销
            cpnId = checkPlan.getCpnId();
        }

        JsonResult jsonResult = new JsonResult();
        jsonResult.setType(JsonResult.ResultType.SUCCESS);
        jsonResult.setData(cpnId);
        return jsonResult;
    }

    /**
     * 盘点计划明细 页面
     *
     * @param cpnId
     * @param model
     * @return 页面路径
     */
    @GetMapping("/planDetail/{cpnId}")
    public String view(@PathVariable("cpnId") Integer cpnId, Model model) {
        model.addAttribute("checkPlan", checkPlanDao.selectByPrimaryKey(cpnId));
        return "material/matWarManage/CheckPlan/planDetail";
    }

    /**
     * 盘点明细查询
     *
     * @param checkDetailModel 查询条件
     * @return
     */
    @ResponseBody
    @PostMapping("/planDetailList")
    public JsonResult findPlanDetailByQuery(MCheckDetailModel checkDetailModel) {
        startPage();
        List<MCheckDetailModel> list = checkPlanService.findPlanDetailByQuery(checkDetailModel);
        return jsonResult(list);
    }

    /**
     * 获取待盘点明细
     *
     * @param checkDetailModel 查询条件
     * @return
     */
    @ResponseBody
    @PostMapping("/inventoryList")
    public JsonResult findInventoryListByQuery(MCheckDetailModel checkDetailModel) {
        startPage();
        List<MCheckDetailModel> list = checkPlanService.findInventoryListByQuery(checkDetailModel);
        return jsonResult(list);
    }

    /**
     * 添加库存盘点明细（批量）
     *
     * @param list
     * @return
     */
    @ResponseBody
    @PostMapping("/addPlanDetail")
    public JsonResult addPlanDetail(@RequestBody List<MCheckDetail> list) {

        checkPlanService.addPlanDetail(list);
        return success();
    }

    /**
     * 删除库存盘点明细
     *
     * @param cdlId 修改信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/del")
    public JsonResult del(Integer cdlId) {
        return jsonResult(checkDetailDao.deleteByPrimaryKey(cdlId));
    }

    /**
     * 获取盘点计划
     *
     * @param cpnId 盘点计划Id
     * @return
     */
    @ResponseBody
    @PostMapping("/planProcess")
    public JsonResult findCheckPlanByPrimaryKey(Integer cpnId) {
        startPage();
        List<MCheckPlanModel> list = checkPlanDao.findCheckPlanByPrimaryKey(cpnId);
        return jsonResult(list);
    }

    /**
     * 盘点结果录入 页面
     *
     * @param cpnId
     * @param model
     * @return 页面路径
     */
    @GetMapping("/resultSave/{cpnId}")
    public String resultSaveView(@PathVariable("cpnId") Integer cpnId, Model model) {
        model.addAttribute("checkPlan", checkPlanDao.selectByPrimaryKey(cpnId));
        return "material/matWarManage/CheckPlan/resultSave";
    }

    /**
     * 保存盘点结果（单条）
     *
     * @param checkDetail 保存信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/checkResultSingle")
    @RequiresPermissions({"matinv:matcheckresult:edit"})
    public JsonResult checkResultSingle(MCheckDetail checkDetail) {
        return jsonResult(checkPlanService.checkResultSingle(checkDetail));
    }

    /**
     * 导出盘点明细
     *
     * @param checkDetailModel
     * @param response
     */
    @GetMapping("/exportCheckDetail")
    @RequiresPermissions({"matinv:matcheckresult:edit"})
    public void exportCheckDetail(MCheckDetailModel checkDetailModel, HttpServletResponse response) {
        checkPlanService.exportCheckDetail(checkDetailModel, response);
    }

    /**
     * 导入盘点结果 页面
     *
     * @return 页面路径
     */
    @GetMapping("/import")
    @RequiresPermissions({"matinv:matcheckresult:edit"})
    public String plan(Integer cpnId, Model model) {
        model.addAttribute("checkPlan", checkPlanDao.selectByPrimaryKey(cpnId));
        return "material/matWarManage/CheckPlan/import";
    }

    /**
     * 导入盘点结果
     *
     * @param file 导入文件
     * @return JsonResult
     */
    @ResponseBody
    @PostMapping("/importFile")
    @RequiresPermissions({"matinv:matcheckresult:edit"})
    public JsonResult importFile(@RequestParam("file") MultipartFile file, @RequestParam("cpnId") Integer cpnId, HttpServletResponse response) {
        String msg = checkPlanService.importFile(file, cpnId);
        JsonResult jsonResult;

        if ("导入失败".equals(msg)) {
            jsonResult = error();
        } else {
            jsonResult = success();
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 判断文件是否存在
     *
     * @param url 文件路径
     * @return
     */
    @ResponseBody
    @PostMapping("/exist")
    public JsonResult exist(String url) {
        return jsonResult(checkPlanService.exist(url));
    }

    /**
     * 盘亏量大于库存在库量数据
     *
     * @param cpnId
     * @param response
     */
    @GetMapping("/lossExport")
    public void lossExport(@RequestParam(value = "cpnId") Integer cpnId, HttpServletResponse response) {
        checkPlanService.exportLossListByPlanId(cpnId, response);
    }

    /**
     * 清空盘点结果
     *
     * @param cpnId 盘点计划Id
     * @return
     */
    @ResponseBody
    @PostMapping("/clear")
    @RequiresPermissions({"matinv:matcheckresult:edit"})
    public JsonResult clear(Integer cpnId) {

        JsonResult jsonResult = new JsonResult();
        String msg = checkPlanService.clear(cpnId);
        if (msg.contains("清空成功！")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 更新盘点状态
     *
     * @param cpnId 盘点计划Id
     * @return
     */
    @ResponseBody
    @PostMapping("/updateResStatus")
    @RequiresPermissions({"matinv:matcheckresult:edit"})
    public JsonResult updateResStatus(Integer cpnId) {

        JsonResult jsonResult = new JsonResult();
        String msg = checkPlanService.updateResStatus(cpnId);
        if (!msg.contains("失败")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 新增盘点结果
     *
     * @param checkDetail 保存信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/saveNewCheckDetail")
    @RequiresPermissions({"matinv:matcheckresult:edit"})
    public JsonResult saveNewCheckDetail(MCheckDetail checkDetail) {
        JsonResult jsonResult = new JsonResult();
        String msg = checkPlanService.saveNewCheckDetail(checkDetail);
        if (!msg.contains("盘点计划中已存在该物料，不允许二次添加！")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 盘点损益调整 页面
     *
     * @param cpnId
     * @param model
     * @return 页面路径
     */
    @GetMapping("/adjust/{cpnId}")
    public String adjustView(@PathVariable("cpnId") Integer cpnId, Model model) {
        model.addAttribute("checkPlan", checkPlanDao.selectByPrimaryKey(cpnId));
        return "material/matWarManage/CheckPlan/adjust";
    }

    /**
     * 盘点损溢明细
     *
     * @param checkDetailModel 查询条件
     * @return
     */
    @ResponseBody
    @PostMapping("/adjustList")
    public JsonResult findAdjustListByQuery(MCheckDetailModel checkDetailModel) {
        startPage();
        List<MCheckDetailModel> list = adjustmentApplyDao.findAdjustListByQuery(checkDetailModel);
        return jsonResult(list);
    }

    /**
     * 损溢明细调整
     *
     * @param adjustmentApplyModel 查询条件
     * @return
     */
    @ResponseBody
    @PostMapping("/adjustStockList")
    public JsonResult findAdjustStockListByQuery(MAdjustmentApplyModel adjustmentApplyModel) {
        startPage();
        List<MAdjustmentApplyModel> list = checkPlanService.findAdjustStockListByQuery(adjustmentApplyModel);
        return jsonResult(list);
    }

    /**
     * 保存损益调整记录（盘亏）
     *
     * @param adjustmentApply 调整记录
     * @return
     */
    @ResponseBody
    @PostMapping("/adjustLoss")
    @RequiresPermissions({"matinv:matcheckadj:edit"})
    public JsonResult adjustLoss(MAdjustmentApply adjustmentApply) {

        JsonResult jsonResult = new JsonResult();
        String msg = checkPlanService.adjustLoss(adjustmentApply);
        if (!msg.contains("！")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        } else {
            jsonResult.setType(JsonResult.ResultType.ERROR);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 保存损益调整记录（盘盈）
     *
     * @param adjustmentApply 调整记录
     * @return
     */
    @ResponseBody
    @PostMapping("/adjustProfit")
    @RequiresPermissions({"matinv:matcheckadj:edit"})
    public JsonResult adjustProfit(MAdjustmentApply adjustmentApply) {
        return jsonResult(checkPlanService.adjustProfit(adjustmentApply));
    }

    /**
     * 更新盘点状态
     *
     * @param cpnId 盘点计划Id
     * @return
     */
    @ResponseBody
    @PostMapping("/updateAdjStatus")
    @RequiresPermissions({"matinv:matcheckadj:edit"})
    public JsonResult updateAdjStatus(Integer cpnId) {

        JsonResult jsonResult = new JsonResult();
        String msg = checkPlanService.updateAdjStatus(cpnId);
        if (!msg.contains("损溢表已提交，撤销失败。")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }
    /**
     * 损溢表 页面
     *
     * @param cpnId
     * @param model
     * @return 页面路径
     */
    @GetMapping("/resultCommit/{cpnId}")
    public String resultCommitView(@PathVariable("cpnId") Integer cpnId, Model model) {
        model.addAttribute("checkPlan", checkPlanDao.selectByPrimaryKey(cpnId));
        return "material/matWarManage/CheckPlan/resultCommit";
    }

    /**
     * 损溢明细调整
     *
     * @param adjustmentApplyModel 查询条件
     * @return
     */
    @ResponseBody
    @PostMapping("/resultCommit")
    public JsonResult findResultCommitListByQuery(MAdjustmentApplyModel adjustmentApplyModel) {
        startPage();
        List<MAdjustmentApplyModel> list = checkPlanService.findResultCommitListByQuery(adjustmentApplyModel);
        return jsonResult(list);
    }

    /**
     * 选中行损益汇总
     *
     * @param checkResultCommitSumModel 查询条件
     * @return
     */
    @ResponseBody
    @PostMapping("/resultCommitSum")
    public JsonResult findResultCommitSumByQuery(MCheckResultCommitSumModel checkResultCommitSumModel) {
        return jsonResult(checkPlanService.findResultCommitSumByQuery(checkResultCommitSumModel));
    }

    /**
     * 更新盘点损溢表状态
     *
     * @param cpnId 盘点计划Id
     * @return
     */
    @ResponseBody
    @PostMapping("/updatePalStatus")
    @RequiresPermissions({"matinv:matchecksub:edit"})
    public JsonResult updatePalStatus(Integer cpnId) {

        JsonResult jsonResult = new JsonResult();
        String msg = checkPlanService.updatePalStatus(cpnId);
        if (!msg.contains("盘点损益已经被审核，本次操作失败")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }



}
